import { createRouter, createWebHistory } from 'vue-router'
import { useLoginStore } from '@/store/loginStore'
import { ElMessage } from 'element-plus'

const routes = [
  {
    path: '/',
    component: () => import('../views/layout/index.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('../views/home/index.vue'),
        meta: {
          title: '首页',
          icon: 'House'
        }
      },
      {
        path: 'admin',
        name: 'Admin',
        component: () => import('../views/admin/list.vue'),
        meta: {
          title: '管理员管理',
          icon: 'Management',
          roles: ['superAdmin']
        }
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('../views/user/list.vue'),
        meta: {
          title: '用户管理',
          icon: 'User'
        }
      },
      {
        path: 'product',
        name: 'Product',
        component: () => import('../views/product/list.vue'),
        meta: {
          title: '商品管理',
          icon: 'Goods'
        }
      },
      {
        path: 'order',
        name: 'Order',
        component: () => import('../views/order/list.vue'),
        meta: {
          title: '订单管理',
          icon: 'List'
        }
      },
      // {
      //   path: 'order/detail/:id',
      //   name: 'OrderDetail',
      //   component: () => import('../views/order/detail.vue'),
      //   meta: {
      //     title: '订单详情',
      //     hidden: true
      //   }
      // },
      {
        path: 'review',
        name: 'Review',
        component: () => import('../views/review/list.vue'),
        meta: {
          title: '评价管理',
          icon: 'ChatDotRound'
        }
      },
      {
        path: 'category',
        name: 'Category',
        component: () => import('../views/category/list.vue'),
        meta: {
          title: '分类管理',
          icon: 'Menu'
        }
      },
      {
        path: '403',
        name: 'Forbidden',
        component: () => import('../views/error/403.vue'),
        meta: {
          title: '没有权限',
          hidden: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: {
      title: '登录',
      noAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? `水果商城管理系统 - ${to.meta.title}` : '水果商城管理系统'
  
  if (to.meta.noAuth) {
    next()
  } else {
    const isLogin = localStorage.getItem('isLogin')
    const userInfoStr = localStorage.getItem('userInfo')
    
    if (isLogin === 'true' && userInfoStr) {
      const userInfo = JSON.parse(userInfoStr)
      
      if (to.meta.roles && to.meta.roles.length > 0) {
        const userRole = userInfo.adminType === 1 ? 'superAdmin' : 'admin'
        if (to.meta.roles.includes(userRole)) {
          next()
        } else {
          ElMessage.error('您没有权限访问该页面')
          if (from.name) {
            next(false)
          } else {
            next('/home')
          }
        }
      } else {
        next()
      }
    } else {
      next('/login')
    }
  }
})

export default router 